package book.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

import book.controller.BookControllerListener;
import book.po.Book;

/**
 * 图书管理主界面
 * MVC的view类
 * @author Dengcj
 *
 */
public class BookFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	private BookControllerListener controllerListener;
	private JPanel tablePanel;
	private int selRow = -1;//选中的表格行索引
	public BookFrame() {
		this.controllerListener = new BookControllerListener();
		this.controllerListener.setBookFrame(this); //向controller注入frame，用于更新view。需要更新的组件需设为frame属性，并生成getter方法。
		this.setJMenuBar(getBookMenuBar());
		this.add(this.getBookModifyToolBar(), BorderLayout.NORTH);
		this.add(this.getBookTablePanel(controllerListener.getBookService().findAllBook()), BorderLayout.CENTER);
		this.setVisible(true);
		this.setSize(480, 550);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.validate();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.validate();
	}
	/**
	 * 重新加载数据表格面板
	 */
	public void refresh(List<Book> bookList) {
		System.out.println("测试——当前图书数量为："+bookList.size());
		this.remove(tablePanel);
		this.add(this.getBookTablePanel(controllerListener.getBookService().findAllBook()), BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
	}
	/**
	 * 查询结果
	 */
	public void showSearchResult(List<Book> result) {
		this.remove(tablePanel);
		this.add(this.getBookTablePanel(result), BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");
	}
	/**
	 * 菜单栏
	 * @return
	 */
	private JMenuBar getBookMenuBar() {
		JMenuItem addBookMenuItem = new JMenuItem("添加图书");
		JMenuItem listBookMenuItem = new JMenuItem("图书列表");
		addBookMenuItem.addActionListener(controllerListener);
		addBookMenuItem.setActionCommand("addBookDialog");
		listBookMenuItem.addActionListener(controllerListener);
		listBookMenuItem.setActionCommand("refreshBookFrame");
		JMenuBar bar = new JMenuBar();
		JMenu fileMenu = new JMenu("图书管理");
		fileMenu.add(addBookMenuItem);
		fileMenu.add(listBookMenuItem);
		bar.add(fileMenu);
		return bar;
	}
	/**
	 * 工具栏
	 * @return
	 */
	private JToolBar getBookModifyToolBar() {
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.setBorder(new BevelBorder(BevelBorder.RAISED));
		toolBar.setAlignmentX(RIGHT_ALIGNMENT);
		
		JButton addBookButton = new JButton("添加");
		ImageIcon addIcon = new ImageIcon(BookFrame.class.getResource("/bookAdd.jpg"));
		addBookButton.setIcon(addIcon);
		JButton searchBookButton = new JButton("查询");
		ImageIcon searchIcon = new ImageIcon(BookFrame.class.getResource("/bookSearch.jpg"));
		searchBookButton.setIcon(searchIcon);
		JButton updateBookButton = new JButton("修改");
		ImageIcon updateIcon = new ImageIcon(BookFrame.class.getResource("/bookUpdate.jpg"));
		updateBookButton.setIcon(updateIcon);
		JButton deleteBookButton = new JButton("删除");
		ImageIcon deleteIcon = new ImageIcon(BookFrame.class.getResource("/bookDelete.jpg"));
		deleteBookButton.setIcon(deleteIcon);
		
		addBookButton.addActionListener(controllerListener);
		searchBookButton.addActionListener(controllerListener);
		updateBookButton.addActionListener(controllerListener);
		deleteBookButton.addActionListener(controllerListener);
		
		addBookButton.setActionCommand("addBookDialog");
		searchBookButton.setActionCommand("searchBookDialog");
		updateBookButton.setActionCommand("updateBookDialog");
		deleteBookButton.setActionCommand("deleteBook");
		
		toolBar.add(addBookButton);
		toolBar.add(new JToolBar.Separator());
		toolBar.add(searchBookButton);	
		toolBar.add(new JToolBar.Separator());
		toolBar.add(updateBookButton);
		toolBar.add(new JToolBar.Separator());
		toolBar.add(deleteBookButton);
		return toolBar;
	}
	/**
	 * 图书列表面板
	 * 封装目的：刷新table数据。
	 * @return
	 */
	private JPanel getBookTablePanel(List<Book> bookList) {
		tablePanel = new JPanel();
		String titles[] = {"ISBN", "书名",  "单价" };
		Object data[][] = new Object[bookList.size()][3];
		Iterator<Book> iter = bookList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Book book = iter.next();
			data[i][0] = book.getIsbn();
			data[i][1] = book.getName();
			data[i][2] = book.getPrice();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data,titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14)); //表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); //表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); //表头高度
		table.setRowHeight(table.getRowHeight()+15); //表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //表格只能选择一行
		table.repaint();
		tablePanel.add(new JScrollPane(table));
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				selRow = table.getSelectedRow();  //得到选中的行
				int isbn = Integer.parseInt(table.getValueAt(selRow, 0).toString().trim());
				String name = table.getValueAt(selRow, 1).toString().trim();
				double price = Double.parseDouble(table.getValueAt(selRow, 2).toString().trim());
				System.out.println("Isbn="+isbn+",name="+name+",price="+price);
			}
		});
		return tablePanel;
	}
	public BookControllerListener getControllerListener() {
		return controllerListener;
	}
	public int getSelRow() {
		return selRow;
	}
}
